import 'package:Note_App/models/note.dart';
import 'package:Note_App/utils/database_helper.dart';
import 'package:Note_App/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class CreateNote extends StatefulWidget {
  final Note note;

  CreateNote(this.note);

  @override
  _CreateNoteState createState() => _CreateNoteState(note);
}

class _CreateNoteState extends State<CreateNote> {
  DatabaseHelper _helper = DatabaseHelper();
  Note note;
  String title = '';
  String content = '';

  _CreateNoteState(this.note);

  TextEditingController _titleController = new TextEditingController();
  TextEditingController _noteController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    _titleController.text = this.note.title;
    _noteController.text = this.note.content;

    return WillPopScope(
      onWillPop: () {
        goToLastScreen();
        return Future.value(false);
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(note.id == null ? '添加笔记' : '编辑笔记'),
          actions: <Widget>[
            FlatButton(
              child: Icon(
                Icons.done,
                color: Colors.black,
              ),
              onPressed: () {
                title = _titleController.text;
                content = _noteController.text;

                if (title == '') {
                  Toast.toast(context, msg: '请输入标题');
                  return false;
                }
                if (content == '') {
                  Toast.toast(context, msg: '请输入笔记内容');
                  return false;
                }
                setState(() {
                  if (note.id == null) {
                    _saveToDatabase();
                  } else {
                    _updateNoteToDatabase();
                  }
                });
              },
            ),
          ],
        ),
        backgroundColor: Colors.white,
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListView(
            children: <Widget>[
              TextField(
                controller: _titleController,
                maxLength: 256,
                decoration:
                    InputDecoration(labelText: '标题', hintText: '添加笔记标题'),
                onChanged: (value) {
                  updateTitle(value);
                },
              ),
              SizedBox(
                height: 10.0,
              ),
              Card(
                elevation: 5.0,
                child: Padding(
                  padding:
                      const EdgeInsets.only(top: 8.0, left: 18.0, right: 18.0),
                  child: new ConstrainedBox(
                      constraints:
                          BoxConstraints(minHeight: 325, maxWidth: 200),
                      child: TextField(
                        controller: _noteController,
                        maxLines: null,
                        decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: "写一条笔记吧....",
                        ),
                        onChanged: (value) {
                          updateContent(value);
                        },
                      )),
                ),
              ),
              SizedBox(
                height: 10.0,
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _showAlertDialog(String title, String message) {
    AlertDialog alertDialog = AlertDialog(
      title: Text(title),
      content: Text(message),
    );

    showDialog(context: context, builder: (_) => alertDialog);
  }

  void goToLastScreen() {
    Navigator.pop(context, true);
  }

  _saveToDatabase() async {
    goToLastScreen();
    note.date = DateFormat.yMMMd().format(DateTime.now());
    int response;
    if (note.id != null) {
    } else {
      response = await _helper.insert(note);
    }
    if (response != 0) {
//      _showAlertDialog('Status', 'Note Saved');
    } else {
      _showAlertDialog('Status', 'Unable to Save');
    }
  }

  _updateNoteToDatabase() {
    goToLastScreen();
    _helper.update(note);
  }

  updateTitle(String value) {
    note.title = value;
  }

  updateContent(String value) {
    note.content = value;
  }
}
